<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>主页</title>
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/plugins/build/css/app.css" media="all">
</head>
<body>
<div class="container">
    <div class="module-title">
        <span class="layui-breadcrumb">
            <a href="javascript:history.go(-1);" class="back">
                <i class="layui-icon back">&#xe603;</i>
                <span class="moduleName">编辑文章</span>
            </a>
        </span>
    </div>
    <form class="layui-form custom_search_form" action="">
        <input type="hidden" name="contentId" value=""/>
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input style="width: 500px;" type="text" name="contentTitle" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div id="editor"></div>
        <div class="layui-form-item content_btn">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="contentForm">立即提交</button>
            </div>
        </div>
    </form>
</div>
<script src="/plugins/layui/layui.js"></script>
<script src="/module/plugins/editor/wangEditor.min.js"></script>
<script src="/plugins/common.js"></script>
<script>
    //保存
    var saveUrl = SERVER_URL+"/cmsContent/edit";
    var detailUrl = SERVER_URL+"/cmsContent/detail";
    var uploadImgUrl = SERVER_URL+"/cmsContent/upload";

    layui.use(['jquery', 'form'], function() {
        var $ = layui.jquery;
        var form = layui.form;

        //创建编辑器
        var E = window.wangEditor;
        var editor = new E('#editor');
        editor.customConfig.uploadImgShowBase64 = true;
        //上传图片
        editor.customConfig.uploadImgServer = uploadImgUrl;
        editor.customConfig.uploadImgMaxLength = 10;
        // 隐藏“网络图片”tab
        editor.customConfig.showLinkImg = false;
        editor.customConfig.uploadFileName = 'image';
        editor.customConfig.uploadImgHooks = {
            customInsert: function (insertImg, result, editor) {
                insertImg(result.data);
            }
        }
        editor.create();

        //初始化表单
        function initForm(data) {
            $("input[name='contentId']").val(data.contentId);
            $("input[name='contentTitle']").val(data.contentTitle);
            editor.txt.html(data.content);
        }

        //判断新增or修改
        if(urlParam("id")){
            layer.load(2, {time: 5*1000});
            $.ajax({
                type: 'GET',
                url: detailUrl,
                data: {id: urlParam("id")},
                success: function (result) {
                    layer.closeAll();
                    if(result.code == 20000){
                        initForm(result.data);
                    }else{
                        layer.msg(result.msg, {time: 1500});
                    }
                }
            });
        }

        //编辑文章
        form.on('submit(contentForm)', function(data){
            if(editor.txt.text() && editor.txt.text() != ""){
                layer.load(2, {time: 5*1000});
                $.ajax({
                    type: 'POST',
                    url: saveUrl,
                    data: {contentId:data.field.contentId,contentTitle: data.field.contentTitle, content:editor.txt.html()},
                    success: function (result) {
                        layer.closeAll();
                        if(result.code == 20000){
                            layer.msg('保存成功', {icon: 1,time: 1000}, function(){
                                history.go(-1);
                            })
                        }else{
                            layer.msg(result.msg, {time: 1500});
                        }
                    }
                });
            }
            return false;
        });
    });

    //地址栏参数
    function urlParam(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)	return unescape(r[2]);
        return null;
    }
</script>
</body>

</html>
